package com.papers.juan.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.papers.common.annotation.Log;
import com.papers.common.annotation.RepeatSubmit;
import com.papers.common.core.controller.BaseController;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.domain.R;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.core.validate.AddGroup;
import com.papers.common.core.validate.EditGroup;
import com.papers.common.enums.BusinessType;
import com.papers.common.helper.LoginHelper;
import com.papers.common.utils.poi.ExcelUtil;
import com.papers.juan.domain.bean.GenerateSaveBean;
import com.papers.juan.domain.bean.QuestionQueryBean;
import com.papers.juan.domain.bean.QuestionTypePropertyBean;
import com.papers.juan.domain.bo.TJuanGenerateBo;
import com.papers.juan.domain.vo.TJuanGenerateDetailVo;
import com.papers.juan.domain.vo.TJuanGenerateVo;
import com.papers.juan.service.ITJuanGenerateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.List;

/**
 * 组卷
 *
 * @author papers
 * @date 2022-10-08
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/juan/juanGenerate")
public class TJuanGenerateController extends BaseController {

    private final ITJuanGenerateService iTJuanGenerateService;

    /**
     * 获取组卷列表
     *
     * @param bo        过滤条件
     * @param pageQuery 分页推荐
     * @return 组卷列表
     */
    @SaCheckPermission("juan:juanGenerate:list")
    @GetMapping("/list")
    public TableDataInfo<TJuanGenerateVo> list(TJuanGenerateBo bo, PageQuery pageQuery) {
        TableDataInfo<TJuanGenerateVo> list = iTJuanGenerateService.queryJuanGenerateList(bo, pageQuery);
        log.debug("{}获取了组卷列表，出参：{}", LoginHelper.getUsername(), list);
        return list;
    }

    /**
     * 根据组卷id获取组卷详情
     *
     * @param generateId 组卷id
     * @return 组卷详情
     */
    @GetMapping("/queryJuanGenerateDetailById")
    public R<TJuanGenerateDetailVo> queryJuanGenerateDetailById(@NotNull(message = "组卷id不能为空")
                                                                @PathParam("generateId") String generateId) {
        TJuanGenerateDetailVo detail = iTJuanGenerateService.queryJuanGenerateDetailById(generateId);
        log.debug("{}根据组卷id获取了组卷详情，出参：{}", LoginHelper.getUsername(), detail);
        return R.ok(detail);
    }

    /**
     * 获取题量配比列表
     *
     * @param questionQueryBean 题目查询bean
     * @return 题量配比列表
     */
    @GetMapping("/queryQuestionCountProportions")
    public R<List<QuestionTypePropertyBean>> queryQuestionCountProportions(QuestionQueryBean questionQueryBean) {
        List<QuestionTypePropertyBean> list = iTJuanGenerateService.queryQuestionCountProportions(questionQueryBean);
        log.debug("{}获取了题量配比列表，出参：{}", LoginHelper.getUsername(), list);
        return R.ok(list);
    }

    /**
     * 获取多试卷的题量配比列表
     *
     * @param paperIds 试卷id列表
     * @return 题量配比列表
     */
    @GetMapping("/queryPapersQuestionCountProportions/{paperIds}")
    public R<List<QuestionTypePropertyBean>> queryPapersQuestionCountProportions(@NotNull(message = "试卷id列表不能为空")
                                                                                 @PathVariable String[] paperIds) {
        List<QuestionTypePropertyBean> list = iTJuanGenerateService.queryPapersQuestionCountProportions(Arrays.asList(paperIds));
        log.debug("{}获取了题量配比列表，出参：{}", LoginHelper.getUsername(), list);
        return R.ok(list);
    }

    /**
     * 自动组卷
     *
     * @param generateId 组卷id
     * @return 试卷详情列表
     */
    @GetMapping("/autoGeneratePaper")
    public R<Void> autoGeneratePaper(@PathParam("generateId") String generateId) {
        return toAjax(iTJuanGenerateService.autoGeneratePaper(generateId));
    }

    /**
     * 导出组卷列表
     */
    @SaCheckPermission("juan:juanGenerate:export")
    @Log(title = "组卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TJuanGenerateBo bo, HttpServletResponse response) {
        List<TJuanGenerateVo> list = iTJuanGenerateService.queryList(bo);
        ExcelUtil.exportExcel(list, "组卷", TJuanGenerateVo.class, response);
    }

    /**
     * 获取组卷详细信息
     *
     * @param generateId 主键
     */
    @SaCheckPermission("juan:juanGenerate:query")
    @GetMapping("/{generateId}")
    public R<GenerateSaveBean> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable String generateId) {
        return R.ok(iTJuanGenerateService.queryById(generateId));
    }

    /**
     * 新增组卷
     */
    @SaCheckPermission("juan:juanGenerate:add")
    @Log(title = "组卷", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GenerateSaveBean saveBean) {
        return toAjax(iTJuanGenerateService.insertByBo(saveBean) ? 1 : 0);
    }

    /**
     * 修改组卷
     */
    @SaCheckPermission("juan:juanGenerate:edit")
    @Log(title = "组卷", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GenerateSaveBean saveBean) {
        return toAjax(iTJuanGenerateService.updateByBo(saveBean) ? 1 : 0);
    }

    /**
     * 删除组卷
     *
     * @param generateIds 主键串
     */
    @SaCheckPermission("juan:juanGenerate:remove")
    @Log(title = "组卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{generateIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] generateIds) {
        return toAjax(iTJuanGenerateService.deleteWithValidByIds(Arrays.asList(generateIds), true) ? 1 : 0);
    }
}
